package com.mgface.zookeeper;

import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class Test {
	private static Logger log= Logger.getLogger(Test.class);
	public static void main(String[] args) throws Exception {
		CountDownLatch cdl = new CountDownLatch(1);
		ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 2_000, e->{
			log.info("接收到的event:"+e);
			if(e.getState()==KeeperState.SyncConnected){
				cdl.countDown();
			}
		});
		log.info("ZK状态:"+zk.getState().toString());
		cdl.await();
		log.info("zookeeper session established");
		String path = zk.create("/zk", "zk01".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		log.info("success create znode:"+path);
		String path2 = zk.create("/zk/001~", "001".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		log.info("success create znode2:"+path2);
		Thread.sleep(30_000);
	}
}	
